package run.vue.iblog.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import run.vue.iblog.entity.Book;
import run.vue.iblog.mapper.BookMapper;
import run.vue.iblog.service.BookService;
import run.vue.iblog.service.CategoryService;

import java.util.List;

@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book>  implements BookService {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private BookMapper bookMapper;

    @Override
    public List<Book> listAll() {
        return bookMapper.findAll();
    }

    @Override
    public void add(Book book) {
        bookMapper.addBook(book);
    }

    @Override
    public void update(Book book) {
        bookMapper.updateBook(book);
    }

    @Override
    public void deleteById(int id) {
        bookMapper.deleteById(id);
    }

    @Override
    public List<Book> listByCategory(int cid) {
//        System.out.println(cid);
//        Category category = categoryService.get(cid);
        return bookMapper.findAllByCategoty(cid);
    }

    @Override
    public List<Book> search(String keywords) {
        System.out.println(bookMapper.findAllByTitleLikeOrAuthorLike('%' + keywords + '%', '%' + keywords + '%'));
        return bookMapper.findAllByTitleLikeOrAuthorLike('%' + keywords + '%', '%' + keywords + '%');
    }

    @Override
    public int getTotalBooks() {
        return bookMapper.totalBooks();
    }
}
